package com.zero.clinic.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.clinic.pojo.*;
import com.zero.clinic.service.DictionaryService;
import com.zero.clinic.service.RecipeService;
import com.zero.clinic.utils.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/recipe")
public class RecipeController {
    @Resource
    private RecipeService recipeService;

    @Resource
    private DictionaryService dictionaryService;

    /**
     * @function 进入处方费管理模块
     * @return
     */
    @RequiresPermissions("recipe:recipePage")
    @RequestMapping("/recipePage")
    public String recipePage(){
        return "recipePage";
    }

    /**
     * @function 处方费列表
     * @param pageNum
     * @param pageSize
     * @param keywords
     * @return
     */
    @ResponseBody
    @RequiresPermissions("recipe:list")
    @RequestMapping("/list")
    public PageInfo<Recipe> recipeList(@RequestParam(defaultValue = "1") int pageNum,
                                   @RequestParam(defaultValue = "10") int pageSize, String keywords){
        PageHelper.startPage(pageNum, pageSize);
        // 1.查出所有的Recipe信息
        RecipeExample example = new RecipeExample();
        // 2 根据搜索关键词查询
        if (StringUtils.isNotBlank(keywords)){
            // 2.1 根据处方费名称查询
            RecipeExample.Criteria criteria1= example.createCriteria();
            criteria1.andRecipeNameLike("%"+ keywords +"%");
            // 2.2 根据处方类型名称查询
            RecipeExample.Criteria criteria2 = example.or();
            criteria2.andRecipeTypeNameLike("%"+ keywords +"%");
        }
        List<Recipe> recipes = recipeService.selectByExample(example);
        // 3.以PageInfo对象返回给前端
        PageInfo<Recipe> pageInfo = new PageInfo<>(recipes);
        return pageInfo;
    }

    /**
     * @function 编辑处方费页面
     * @param recipeId
     * @param m
     * @return
     */
    @RequestMapping("/edit")
    public String recipeEdit(Long recipeId, Model m){

        // 1.如果前端页面需要更新处方费信息，则会传一个需要更新的recipeId过来。这里需要通过recipeId查出更新信息后共享给前端页面
        if (recipeId != null) {
            Recipe recipe = recipeService.selectByPrimaryKey(recipeId);
            m.addAttribute("recipe",recipe);
        }

        // 2.将处方费类型的基础数据查出来共享给前端页面
        List<Dictionary> dictionaries = dictionaryService.selectDictionariesByParentName(Constant.DICTIONARY_MEDICINE_TYPE);
        m.addAttribute("dictionaries", dictionaries);

        return "recipeEdit";
    }

    /**
     * @function 新增处方费
     * @param recipe
     * @return
     */
    @ResponseBody
    @RequestMapping("/insert")
    @RequiresPermissions("recipe:insert")
    public MessageObject recipeInsert(Recipe recipe){
        //1.插入数据
        System.out.println("需要新增的处方费：" + recipe);
        int row = recipeService.insert(recipe);
        if (row > 0) {
            return MessageObject.createMo(1,"新增数据成功");
        }
        return MessageObject.createMo(0,"新增数据失败，请联系技术人员");
    }

    /**
     * @function 更新处方费
     * @param recipe
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("recipe:update")
    public MessageObject recipeUpdate(Recipe recipe){
        //1.更新数据
        System.out.println("需要更新的处方费" + recipe);
        int row = recipeService.updateByPrimaryKeySelective(recipe);
        if (row > 0) {
            return MessageObject.createMo(1,"更新数据成功");
        }
        return MessageObject.createMo(0,"更新数据失败，请联系技术人员");
    }

    /**
     * @function 检查处方费名是否重复
     * @param recipeName
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkRecipeName")
    @RequiresPermissions("recipe:checkRecipeName")
    public boolean recipeCheckRecipeName(String recipeName){
        RecipeExample example = new RecipeExample();
        RecipeExample.Criteria criteria = example.createCriteria();
        criteria.andRecipeNameEqualTo(recipeName);
        List<Recipe> recipes = recipeService.selectByExample(example);
        return recipes.size() > 0 ? false : true;
    }

    /**
     * @function 删除处方费
     * @param recipeId
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("recipe:delete")
    public MessageObject recipeDelete(Long recipeId){
        //1.根据recipeId删除数据
        int row = recipeService.deleteByPrimaryKey(recipeId);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败，请联系技术人员");
    }

    /**
     * @function 批量删除处方费
     * @param recipeIds
     * @return
     */
    @ResponseBody
    @RequestMapping("/batchdel")
    @RequiresPermissions("recipe:batchdel")
    public MessageObject recipeBatchDelete(String recipeIds){
        String[] recipeIdsArr = recipeIds.split(",");
        //1.根据recipeIdsArr删除数据
        int row = recipeService.deleteByIds(recipeIdsArr);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败");
    }

    /**
     * @function: 通过前端选中的处方ID查询对应处方数据并返回
     * @param recipeId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getRecipeInfoById")
    public Recipe getRecipeInfoById(Long recipeId){
        Recipe recipe = recipeService.selectByPrimaryKey(recipeId);
        return recipe;
    }

}
